﻿Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Public Class PdfManager

    Private control As New FindJobOfferControlKrypton

    '===============================================================
    ' OverRidden Constructor of PdfManager
    '---------------------------------------------------------------
    'Purpose : Constructor of PdfManager with control attribute
    '
    ' Author : Kwtteras Dimitrios, 4 January 2013
    '===============================================================

    Public Sub New(ByVal control)
        Me.control = control
    End Sub

    '===============================================================
    ' Export data from DataGridView to PdfTable
    '---------------------------------------------------------------
    'Purpose : Export data from DataGridView in FindJobOffers Control
    '          to Pdf document with the use of functions and subs
    '
    ' Author : Kwtteras Dimitrios, 4 January 2013
    '===============================================================

    Public Sub ExportDataToPDFTable(ByVal filePath As String)

        Dim doc As New Document(iTextSharp.text.PageSize.A4, 40, 40, 40, 10)
        Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(filePath, FileMode.Create))
        doc.Open()

        Dim font12Normal As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

        'add phrases in doc
        addPhrase(font12Normal, doc)

        'new pdfTable
        Dim pdfTable = newPdfTable()

        'add titles in cell
        addTitles(pdfTable)

        'pdfCell Decleration
        Dim PdfPCell As PdfPCell = Nothing

        'get data from datagridview
        Dim dataTable = GetDataTable()

        'export data from dataTable to pdfTable
        If dataTable IsNot Nothing Then
            'Now add data from datatable to pdf table
            For rows As Integer = 0 To dataTable.Rows.Count - 1
                For column As Integer = 0 To dataTable.Columns.Count - 1
                    PdfPCell = New PdfPCell(New Phrase(dataTable.Rows(rows)(column).ToString(), font12Normal))
                    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                    pdfTable.AddCell(PdfPCell)
                Next
            Next
            'Adding pdfTable to the pdfdocument
            doc.Add(pdfTable)
        End If
        doc.Close()

    End Sub

    '===============================================================
    ' Add Phrases in Pdf document
    '---------------------------------------------------------------
    'Purpose : Creating and adding Phrases in Pdf document
    '
    ' Author : Kwtteras Dimitrios, 4 January 2013
    '===============================================================

    Private Sub addPhrase(ByRef font12Normal, ByVal doc)

        Dim paragraph As New Paragraph
        Dim font12BoldRed As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.UNDERLINE Or iTextSharp.text.Font.BOLDITALIC, BaseColor.RED)
        Dim p1, p2, p3, p4, p5 As New Phrase
        p1 = New Phrase(New Chunk("Job Offer results with these options:", font12BoldRed))
        p2 = New Phrase(New Chunk("Job Type: " + control.cboJobType.Text, font12Normal))
        p3 = New Phrase(New Chunk("Min Salary: " + control.txtMinSalary.Text, font12Normal))
        p4 = New Phrase(New Chunk("Country: " + control.cboCountry.Text, font12Normal))
        p5 = New Phrase(New Chunk("City: " + control.cboCity.Text, font12Normal))

        doc.Add(p1)
        doc.Add(paragraph)
        doc.Add(p2)
        doc.Add(paragraph)
        doc.Add(p3)
        doc.Add(paragraph)
        doc.Add(p4)
        doc.Add(paragraph)
        doc.Add(p5)

    End Sub

    '===============================================================
    ' Making new PdfTable
    '---------------------------------------------------------------
    'Purpose : Making new PdfTable for saving there later data of 
    '          DataGridView
    '
    ' Author : Kwtteras Dimitrios, 4 January 2013
    '===============================================================

    Private Function newPdfTable() As PdfPTable

        'Create instance of the pdf table and set the number of column in that table
        Dim pdfTable = New PdfPTable(2)

        'pdfTable view properties
        pdfTable.TotalWidth = 490.0F
        'fix the absolute width of the table
        pdfTable.LockedWidth = True
        'relative col widths in proportions - 1,1
        Dim widths As Single() = New Single() {1.0F, 1.0F}
        pdfTable.SetWidths(widths)
        pdfTable.HorizontalAlignment = 1 ' 0 --> Left, 1 --> Center, 2 --> Right
        pdfTable.SpacingBefore = 2.0F

        Return pdfTable

    End Function

    '===============================================================
    ' Titles for PdfTable
    '---------------------------------------------------------------
    'Purpose : Creating Cells for PdfTable and adding there titles
    '
    ' Author : Kwtteras Dimitrios, 4 January 2013
    '===============================================================

    Private Sub addTitles(ByVal pdfTable)

        Dim font12Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)

        'pdfTable titles
        Dim titleTableArray() As String = _
         {"Job Offer Description", "Rank"}

        'pdfCell Decleration
        Dim PdfPCell As PdfPCell = Nothing

        'add titles to pdfcells
        For i As Integer = 0 To 1
            'Assigning values to each cell as phrases
            PdfPCell = New PdfPCell(New Phrase(New Chunk(titleTableArray(i), font12Bold)))
            'Alignment of phrase in the pdfcell
            PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
            'Add pdfcell in pdftable
            pdfTable.AddCell(PdfPCell)
        Next

    End Sub

    '===============================================================
    ' Creating DataTable
    '---------------------------------------------------------------
    'Purpose : Creating DataTable and adding data from DataGridView
    '          in its Rows
    '
    ' Author : Kwtteras Dimitrios, 4 January 2013
    '===============================================================

    Private Function GetDataTable() As DataTable

        Dim dataTable As New DataTable("MyDataTable")
        
        'Create DataColumns for DataTable
        For i As Integer = 0 To 1
            Dim dataColumn_i As New DataColumn(control.DataGridViewJobOffers.Columns(i).HeaderText.ToString(), GetType(String))
            dataTable.Columns.Add(dataColumn_i)
        Next

        'Add some row to newly created dataTable
        Dim dataRow As DataRow
        For i As Integer = 0 To control.DataGridViewJobOffers.Rows.Count - 1
            dataRow = dataTable.NewRow()
            'here you give the number of the column in datagrid you want to view in pdf
            dataRow(control.DataGridViewJobOffers.Columns(0).HeaderText.ToString()) = control.DataGridViewJobOffers.Rows(i).Cells(13).Value.ToString()
            dataRow(control.DataGridViewJobOffers.Columns(1).HeaderText.ToString()) = control.DataGridViewJobOffers.Rows(i).Cells(24).Value.ToString()

            dataTable.Rows.Add(dataRow)
        Next

        dataTable.AcceptChanges()
        Return dataTable

    End Function

End Class
